package com.yty.system.oauth.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.api.R;
import com.yty.system.oauth.config.UserDetailService;
import com.yty.system.oauth.entity.ProjectInfo;
import com.yty.system.oauth.entity.SysUser;
import com.yty.system.oauth.service.IUserService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest request) {
        SysUser currentUser = UserDetailService.getCurrentUser();
        return currentUser;
    }

    @GetMapping("/list")
    public Object list() {
        List<SysUser> list = userService.list();
        try {
            if (isExistIndex("projectlist")) {
                BulkRequest bulk = new BulkRequest();
                for (SysUser sysUser : list) {
                    IndexRequest request = new IndexRequest("projectlist").id(sysUser.getId());
                    String json = JSONObject.toJSONString(sysUser);
                    request.source(json, XContentType.JSON);
                    bulk.add(request);
                }
                restHighLevelClient.bulk(bulk, RequestOptions.DEFAULT);
            }else {
                System.out.println("索引不存在");
            }
            System.out.println("操作成功");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return R.ok("操作成功");
    }

    @GetMapping("queryById")
    public Object queryById(String id)  throws Exception{
        GetRequest request = new GetRequest("projectlist",id);
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        String json = response.getSourceAsString();
        return R.ok(json);
    }


    public boolean isExistIndex(String indices){
        try {
            boolean isExist = restHighLevelClient.indices().exists(new GetIndexRequest(indices),
                    RequestOptions.DEFAULT) && restHighLevelClient.count(new CountRequest(indices),
                    RequestOptions.DEFAULT).getCount() > 0;
            if (isExist) {
                return true;
            }
            CreateIndexRequest request = new CreateIndexRequest(indices);
            //设置请求中的参数
            restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
